package com.itheima.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.alibaba.fastjson.JSON;
import com.itheima.constant.MessageConstant;
import com.itheima.constant.RedisMessageConstant;
import com.itheima.entity.Result;
import com.itheima.pojo.Member;
import com.itheima.service.MemberService;
import com.itheima.utils.SMSUtils;
import com.itheima.utils.ValidateCodeUtils;
import com.tencentcloudapi.tbp.v20190627.models.ResponseMessage;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import redis.clients.jedis.JedisPool;

import javax.servlet.http.Cookie;
import java.util.Date;
import java.util.Map;

@RestController
@RequestMapping("/validateCode")
public class ValidateCodeController {
//    @Autowired
//    private JedisPool jedisPool;
    @Reference
    private MemberService memberService;

    @RequestMapping("/send4Login")
    public Result send4Login(@RequestParam("telephone") String tel){
        String validate = ValidateCodeUtils.generateValidateCode4String(6);
        String[] param={validate};
        String[] phone={tel};
        try {
//            SMSUtils.sendSMS(phone,SMSUtils.VALIDATE_CODE,param);
            // 保存到redis并设置超时时间
//            jedisPool.getResource().setex(tel + RedisMessageConstant.SENDTYPE_LOGIN,300,validate);
            return new Result(true, MessageConstant.SEND_VALIDATECODE_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.SEND_VALIDATECODE_FAIL);
        }
    }
    @RequestMapping("/check")
    public Result check(@RequestBody  Map map){
        String tel = (String)  map.get("telephone");
        String validateCode = (String)  map.get("validateCode");
        if (tel==null||validateCode==null){
            return new Result(false,MessageConstant.TELEPHONE_VALIDATECODE_NOTNULL);
        }
//        String Rcode = jedisPool.getResource().get(tel + RedisMessageConstant.SENDTYPE_LOGIN);
        Boolean Rcode=true;
        if (Rcode==true||Rcode!=null&&Rcode.equals(validateCode)){
            //这个人是不是新会员
            Member byPhone = memberService.findByPhone(tel);
            if (byPhone==null){
                // 自动注册为新会员
                Member member = new Member();
                member.setPhoneNumber(tel);
                member.setRegTime(new Date());
                memberService.addMember(member);
                byPhone=member;
            }
            // 向客户端写入cookie
            Cookie cookie = new Cookie("login_member_telephone",tel);
            cookie.setPath("/");
            // 把用户信息放到redis中
            String json = JSON.toJSON(byPhone).toString();
//            jedisPool.getResource().setex(tel,60*30,json);
            //登录成功
            return new Result(true,"true");
        }else {
            return new Result(false,MessageConstant.VALIDATECODE_ERROR);
        }
    }

}
